#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int T;
  cin >> T;
  while (T --) {
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i ++) {
      cin >> a[i];
    }
    vector<vector<int>> dp(n + 1, vector<int>(n + 1));
    for (int l = n - 1; l >= 1; l --) {
      if (a[l] != a[l + 1]) dp[l][l + 1] = 1;
      for (int r = l + 3; r <= n; r += 2) {
        if (a[l] != a[r]) dp[l][r] |= dp[l + 1][r - 1];
        dp[l][r] |= dp[l][l+1] & dp[l+2][r];
      }
    }
  }
}